// const fs = require("fs");
// const exApp = require("../index");
// const multer = require("multer");
// const path = require("path");
// const upload = multer({
//   dest: path.join(path.dirname(__dirname), "./uploads", "publishImgs"),
// });
// exApp.post("/addimg", upload.array("files", 6), async (req, res, next) => {
//   console.log(req.files);

//   res.json({
//     code: 200,
//     msg: "添加成功",
//   });
// });
// const fs = require("fs");
// const exApp = require("../index");
// const path = require("path");
// const multer = require("multer");
// //是nodejs中处理multipart/form-data数据格式(主要用在上传功能中)的中间件
// var formidable = require("formidable"); //blod处理
// //配置diskStorage来控制文件存储的位置以及文件名字等
// var storage = multer.diskStorage({
//   //确定图片存储的位置
//   destination: function (req, file, cb) {
//     cb(null, "./uploads");
//   },
//   // (http://images2017.cnblogs.com/blog/1283058/201802/1283058-20180201154342296-515041615.png)
//   //确定图片存储时的名字,注意，如果使用原名，可能会造成再次上传同一张图片的时候的冲突
//   filename: function (req, file, cb) {
//     let md5s = md5(Date.now() + file.originalname);
//     cb(null, md5s + ".png");
//   },
// });
// var upload = multer({
//   storage: storage,
// });
// const singleMidle = upload.single("files"); //一次处理一张
// const arrMidle = upload.array("arrayFile", 6); //一次最多处理5张
// const fieldsMidle = upload.fields([
//   {
//     name: "fieldSingleFile",
//     maxCount: 1,
//   },
//   {
//     name: "fieldArrayFile",
//     maxCount: 4,
//   },
// ]);
// exApp.post("/addimg", function (req, res, next) {
//   //接收前台POST过来的base64
//   var imgData = req.body.file;
//   console.log(imgData, 111);
//   console.log(req.body);
//   //过滤data:URL
//   var base64Data = imgData.replace(/^data:image\/\w+;base64,/, "");
//   var timestamp = Date.parse(new Date());
//   var dataBuffer = Buffer.from(base64Data, "base64");
//   var name = timestamp.toString() + ".jpg";
//   // 写入的地址
//   fs.writeFile("./uploads/" + name, dataBuffer, function (err) {
//     if (err) {
//       res.send(err);
//     } else {
//       //  res.send("保存成功！");
//       console.log();
//       console.log("服务端接收到了数据啦");
//     }
//   });
// });
// var imgData = req.body.imgData;
// //过滤data:URL
// var base64Data = imgData.replace(/^data:image\/\w+;base64,/, "");
// var dataBuffer = new Buffer(base64Data, 'base64');
// fs.writeFile("image.png", dataBuffer, function(err) {
// if(err){
// res.send(err);
// }else{
// res.send("保存成功！");
// }
// });

// var fs = require("fs"); // 引入fs模块
// var path = "./uploads/" + user_name + "/head_pic/" + Date.now() + ".png"; //路径从app.js级开始找--
// var base64 = base_64_url.replace(/^data:image\/\w+;base64,/, ""); //去掉图片base64码前面部分data:image/png;base64
// var dataBuffer = Buffer.from(base64, "base64"); //把base64码转成buffer对象，
// console.log("dataBuffer是否是Buffer对象：" + Buffer.isBuffer(dataBuffer)); // 输出是否是buffer对象
// fs.writeFile(path, dataBuffer, function (err) {
//   //用fs写入文件
//   if (err) {
//     console.log(err);
//   } else {
//     console.log("写入成功！");
//   }
// });
const exApp = require("../index");
const express = require("express");
const path = require("path");
const multer = require("multer");
let files = [];
const storage = multer.diskStorage({
  //存储的位置
  destination(req, file, cb) {
    cb(null, "./uploads/");
  },
  filename(req, file, cb) {
    console.log(file);
    cb(null, file.originalname);
  },
});
const upload = multer({
  storage,
});
// console.log(upload);
exApp.post("/addimga", upload.single("imgsa"), (req, res, next) => {
  // 接收formData中的参数
  console.log(req);
  // console.log(req.fields);
  // console.log(storage, upload);
  console.log(req.file);
  const url = "http://localhost:8888/uploads/" + req.file.filename;
  res.json({ url });
  // 接收formData中的文件
  // req.files
  console.log(url);
});
// console.log(upload);
exApp.post("/addimgb", upload.single("imgsb"), (req, res, next) => {
  // 接收formData中的参数
  console.log(req);
  // console.log(req.fields);
  // console.log(storage, upload);
  console.log(req.file);
  const url = "http://localhost:8888/uploads/" + req.file.filename;
  res.json({ url });
  // 接收formData中的文件
  // req.files
  console.log(url);
});
exApp.use("/uploads", express.static(__dirname + "/uploads"));
exApp.use(express.static(path.resolve("./public")));
